<template>
  <div class="user-add-main">
    <el-form ref="addForm" :model="adduser" clearable filterable label-width="120px">
      <el-form-item label="测评名称:" prop="measurementName" :rules="addRules.measurementName">
        <el-input v-model="adduser.measurementName" />
      </el-form-item>
      <el-form-item label="测评介绍:" prop="measurementIntroduce" :rules="addRules.measurementIntroduce">
        <el-input type="textarea" v-model="adduser.measurementIntroduce"  />
      </el-form-item>
      <el-form-item label="测评指导语:" prop="measurementInstructions" :rules="addRules.measurementInstructions">
        <el-input type="textarea" v-model="adduser.measurementInstructions" />
      </el-form-item>
      <el-form-item label="岗位:" prop="jobsId" :rules="addRules.jobsId">
        <el-select v-model="adduser.jobsId" placeholder="请选择" @change="getDimension()">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.jobsName"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="测评考题类型:" prop="measurementTypeId" :rules="addRules.measurementTypeId">
        <el-select v-model="adduser.measurementTypeId" placeholder="请选择">
          <el-option
            v-for="item in measurementList"
            :key="item.id"
            :label="item.measurementType"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item v-if="adduser.measurementTypeId==2" label="测评维度:" prop="dimensionId" :rules="addRules.dimensionId">
        <el-select v-model="adduser.dimensionId" placeholder="请选择">
          <el-option
            v-for="item in dimensionList"
            :key="item.id"
            :label="item.dimensionName"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import scaleApi from '@/api/scale'
import dimensionApi from '@/api/dimension'
import typeApi from '@/api/measurementType'
export default {
  data() {
    return {
      adduser:{
        measurementName: "",
        measurementIntroduce:"",
        measurementInstructions:"",
        jobsId: null,
        measurementTypeId: null,
        dimensionId:null
      },
      addRules: {
        measurementName: [{ type: 'String', required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*测评名称不能为空!'))
                } else if(value.length<3){
                  callback(new Error('*请输入测评名称!'))
                } else {
                  this.$refs['addForm'].clearValidate(['measurementName']);
                  callback()
                }
              }
        }],
        measurementIntroduce: [{ type: 'String', required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*测评介绍不能为空!'))
                } else {
                  this.$refs['addForm'].clearValidate(['measurementIntroduce']);
                  callback()
                }
              }
        }],
        measurementInstructions: [{ type: 'String', required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*测评指导语不能为空!'))
                } else {
                  this.$refs['addForm'].clearValidate(['measurementInstructions']);
                  callback()
                }
              }
        }],
        jobsId: [{ type: 'String', required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value==null) {
                  callback(new Error('*岗位不能为空!'))
                } else {
                  this.$refs['addForm'].clearValidate(['jobsId']);
                  callback()
                }
              }
        }],
        measurementTypeId: [{ type: 'String', required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value==null) {
                  callback(new Error('*测评考题类型不能为空!'))
                } else {
                  this.$refs['addForm'].clearValidate(['measurementTypeId']);
                  callback()
                }
              }
        }],
        dimensionId: [{ type: 'String', required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value==null) {
                  callback(new Error('*维度类型不能为空!'))
                } else {
                  this.$refs['addForm'].clearValidate(['dimensionId']);
                  callback()
                }
              }
        }],
      },
      typeList: this.$store.getters.typeList,
      measurementList:[],
      dimensionList:[]
    }
  },
  created() {
    typeApi.query().then(res => {
      this.measurementList = res.data
    })
  },
  methods: {
    getDimension(){
      dimensionApi.queryList(this.adduser.jobsId).then(res => {
        this.dimensionList = res.data
      })
    },
    onSubmit() {
        this.$refs.addForm.validate(valid => {
        if (valid) {
          scaleApi.add(this.adduser).then(res => {
            this.$message.success(res.data)
            this.$emit('closeDisplay')
            this.$emit('getPageList')
            this.$refs.addForm.resetFields()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .user-add-main{
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
